package com.wuliqing.android.zte.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.wuliqing.android.zte.myapplication.R;

/**
 * Created by 10172915 on 2015/7/10.
 */
public class EraserView extends View {
    private static final int MIN_MOVE_DIS = 5;

    private Bitmap fgBitmap, bgBitmap;
    private Canvas mCanvas;
    private Paint mPaint;
    private Path mPath;

    private int screenW, screenH;
    private float preX, preY;

    public EraserView(Context context) {
        this(context, null);
    }

    public EraserView(Context context, AttributeSet set) {
        super(context, set);

        cal(context);

        init(context);
    }

    private void cal(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        screenW = wm.getDefaultDisplay().getWidth();//dm.widthPixels;
        screenH = wm.getDefaultDisplay().getHeight();//dm.heightPixels;
    }

    private void init(Context context) {
        mPath = new Path();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);

        mPaint.setARGB(128, 255, 0, 0);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setStrokeJoin(Paint.Join.ROUND);

        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mPaint.setStrokeWidth(50);

        fgBitmap = Bitmap.createBitmap(screenW, screenH, Bitmap.Config.ARGB_8888);

        mCanvas = new Canvas(fgBitmap);

        mCanvas.drawColor(0xFF808080);

        bgBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.cheese_3);

        bgBitmap = Bitmap.createScaledBitmap(bgBitmap, screenW, screenH, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bgBitmap, 0, 0, null);

        canvas.drawBitmap(fgBitmap, 0, 0, null);

        mCanvas.drawPath(mPath, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.reset();
                mPath.moveTo(x, y);
                preX = x;
                preY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(x - preX);
                float dy = Math.abs(y - preY);
                if (dx >= MIN_MOVE_DIS || dy >= MIN_MOVE_DIS) {
                    mPath.quadTo(preX, preY, (x + preX) / 2, (y + preY) / 2);
                    preX = x;
                    preY = y;
                }
                break;
        }

        invalidate();
        return true;
    }
}
